#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int longestOnes(vector<int>& nums, int k)
    {
        int left = 0, right = 0;
        int n = nums.size();
        int zeroCnt = 0;
        int ret = 0;
        if (nums[left] == 0) ++zeroCnt;
        else ++ret;
        //cout<<"left="<<left<<",right="<<right<<",zero="<<zeroCnt<<endl;
        while (right < n)
        {
            if (zeroCnt <= k)
            {
                ret = max(ret, right - left + 1);
                //cout<<"ret="<<ret<<endl;
                ++right;
                if (right >= n) break;
                //cout<<"left="<<left<<",right="<<right<<",zero="<<zeroCnt<<endl;
                if (nums[right] == 0)
                {
                    ++zeroCnt;
                }
            }
            else
            {
                if (nums[left] == 0)
                {
                    --zeroCnt;
                }
                ++left;
                if (left >= n) break;
                //cout<<"left="<<left<<",right="<<right<<",zero="<<zeroCnt<<endl;
            }
        }
        //cout<<"end"<<endl;
        return ret;
    }
};